package com.yami.shop.platform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.Advertising;
import com.yami.shop.bean.model.IndexImg;
import com.yami.shop.bean.model.Product;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.service.AdvertisingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.Objects;

/**
 * @author wxf
 * @date 2023/6/13 15:51
 */
@RestController
@RequestMapping("/platform/advertising")
public class AdvertisingController {

    @Autowired
    private AdvertisingService advertisingService;

    @GetMapping("/page")
    @Operation(summary = "获取广告")
    public ServerResponseEntity<IPage<Advertising>> page(PageParam<Advertising> page, Advertising param){
        IPage<Advertising> pageBox = advertisingService.getPage(page, param);
        return ServerResponseEntity.success(pageBox);
    }

    @GetMapping("/info/{advertisingId}")
    @PreAuthorize("@pms.hasPermission('platform:advertising:info')")
    @Operation(summary =  "获取信息", description = "获取信息")
    @Parameter(name = "advertisingId", description = "广告id")
    public ServerResponseEntity<Advertising> info(@PathVariable("advertisingId") Long advertisingId) {
        Advertising advertising = advertisingService.getOne(new LambdaQueryWrapper<Advertising>().eq(Advertising::getAdvertisingId, advertisingId));
        return ServerResponseEntity.success(advertising);
    }

    @PostMapping
    @PreAuthorize("@pms.hasPermission('platform:advertising:save')")
    @Operation(summary =  "保存", description = "保存")
    public ServerResponseEntity<Void> save(@RequestBody @Valid Advertising param) {
        advertisingService.save(param);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @PreAuthorize("@pms.hasPermission('platform:advertising:update')")
    @Operation(summary =  "修改", description = "修改")
    public ServerResponseEntity<Void> update(@RequestBody @Valid Advertising param) {
        advertisingService.saveOrUpdate(param);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('platform:advertising:delete')")
    @Operation(summary =  "删除", description = "删除")
    @Parameter(name = "ids", description = "广告id")
    public ServerResponseEntity<Void> delete(@RequestBody Long[] ids) {
        advertisingService.deleteAdvertisingByIds(ids);
        return ServerResponseEntity.success();
    }

}
